import cv2
import os

def extract_all_frames(video_path, output_folder):
    # 创建输出文件夹
    os.makedirs(output_folder, exist_ok=True)

    # 打开视频文件
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        print(f"无法打开视频文件: {video_path}")
        return

    # 获取视频信息
    fps = cap.get(cv2.CAP_PROP_FPS)
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    print(f"视频信息: {video_path}")
    print(f"帧率(FPS): {fps}")
    print(f"总帧数: {total_frames}")

    frame_count = 0
    while True:
        ret, frame = cap.read()
        if not ret:
            break  # 视频结束

        # 保存每一帧
        frame_filename = os.path.join(output_folder, f"frame_{frame_count:04d}.jpg")
        cv2.imwrite(frame_filename, frame)
        frame_count += 1

        # 打印进度（可选）
        if frame_count % 100 == 0:
            print(f"已提取 {frame_count}/{total_frames} 帧...")

    cap.release()
    print(f"完成！共提取 {frame_count} 帧，保存在 {output_folder}")

# 示例：提取单个视频的所有帧
video_path = r"C:\Users\86152\Downloads\苹果树1-1.mp4" # 替换为你的视频路径
output_folder = r"C:\Users\86152\Downloads\video1-1" # 输出文件夹
extract_all_frames(video_path, output_folder)